﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Clock
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        GWB.Forms f = new GWB.Forms();
        Timer t = new Timer();
        private void frmMain_Load(object sender, EventArgs e)
        {
            chkBounceBack.Checked = Properties.Settings.Default.BounceBack;
            chkAllowTransparency.Checked = Properties.Settings.Default.AllowTransparency;
            chkAllowTransparency_CheckedChanged(sender, e);
            radBig.Checked = !Properties.Settings.Default.UseSmall;
            radSmall.Checked = Properties.Settings.Default.UseSmall;
            if (Properties.Settings.Default.UseSmall)
                radSmall_CheckedChanged(sender, e);
            else
                radBig_CheckedChanged(sender, e);
            if (Properties.Settings.Default.KeepFading)
            {
                lblStopFadding.Text = "[STOP FADDING]";
                f.fade(this, 0, 100);
            }
            else
            {
                lblStopFadding.Text = "[START FADDING]";
                this.Opacity = 1;
                f_FadeCompleted();
            }
            PositionStopFadding();
            SetTime();
            PlaceForm();
            PositionClose();
            PositionCornerButton();
            f.FadeCompleted += new FadeDone(f_FadeCompleted);
            t.Tick += new EventHandler(t_Tick);
            t.Enabled = true;
            t.Interval = 1000 - DateTime.Now.Millisecond;
        }

        private void PlaceForm()
        {
            if (Properties.Settings.Default.BounceBack)
            {
                switch (Properties.Settings.Default.LastCorner)
                {
                    case "TR":
                        Position(Place.TopRight);
                        break;
                    case "TL":
                        Position(Place.TopLeft);
                        break;
                    case "BL":
                        Position(Place.BottomLeft);
                        break;
                    case "BR":
                        Position(Place.BottomRight);
                        break;
                }
            }
        }

        void t_Tick(object sender, EventArgs e)
        {
            if (t.Interval != 1000)
                t.Interval = 1000;
            SetTime();
        }

        private void Position(Place p)
        {
            switch (p)
            {
                case Place.TopLeft:
                    this.Location = new Point(0, 0);
                    break;
                case Place.TopRight:
                    this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width, 0);
                    break;
                case Place.BottomLeft:
                    this.Location = new Point(0, Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height);
                    break;
                case Place.BottomRight:
                    this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Size.Width,
                        Screen.PrimaryScreen.WorkingArea.Height - this.Size.Height);
                    break;
            }            
        }

        private void SetTime()
        {
            DateTime dt = DateTime.Now;
            lblTime.Text = dt.Hour + ":" + dt.Minute + ":" + dt.Second;
        }

        void f_FadeCompleted()
        {
            if (this.Opacity == 1)
            {
                if (Properties.Settings.Default.KeepFading)
                {
                    f.fade(this, 100, 0);
                }
            }
            else
                f.fade(this, 0, 100);
        }

        private void frmMain_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                Position(Place.TopRight);

            GWB.Forms.MoveForm(this, e.Button, MousePosition);
        }

        private void lblClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lblClose_MouseEnter(object sender, EventArgs e)
        {
            if (radSmall.Checked)
                lblClose.Font = new Font("Cooper Black", 12);
            else
                lblClose.Font = new Font("Cooper Black", 16);
            PositionClose();
        }

        private void lblClose_MouseLeave(object sender, EventArgs e)
        {
            if (radSmall.Checked)
                lblClose.Font = new Font("Cooper Black", 8);
            else
                lblClose.Font = new Font("Cooper Black", 12);
            PositionClose();
        }

        private void PositionClose()
        {
            if (radSmall.Checked)
                lblClose.Location = new Point((this.Size.Width / 2) - (lblClose.Size.Width / 2), 
                            this.Size.Height - lblClose.Size.Height - 15);
            else
                lblClose.Location = new Point((this.Size.Width / 2) - (lblClose.Size.Width / 2),
                            this.Size.Height - lblClose.Size.Height - 25);
        }

        private void btnPosTopLeft_Click(object sender, EventArgs e)
        {
            Position(Place.TopLeft);
            Properties.Settings.Default.LastCorner = "TL";
            Properties.Settings.Default.Save();
        }

        private void btnPosTopRight_Click(object sender, EventArgs e)
        {
            Position(Place.TopRight);
            Properties.Settings.Default.LastCorner = "TR";
            Properties.Settings.Default.Save();
        }

        private void btnPosBottomLeft_Click(object sender, EventArgs e)
        {
            Position(Place.BottomLeft);
            Properties.Settings.Default.LastCorner = "BL";
            Properties.Settings.Default.Save();
        }

        private void btnPosBottomRight_Click(object sender, EventArgs e)
        {
            Position(Place.BottomRight);
            Properties.Settings.Default.LastCorner = "BR";
            Properties.Settings.Default.Save();
        }

        private void PositionCornerButton()
        {
            btnPosBottomLeft.Location = new Point(0, this.Size.Height - btnPosBottomLeft.Size.Height);
            btnPosBottomRight.Location = new Point(this.Size.Width - btnPosBottomLeft.Size.Width,
                                                    this.Size.Height - btnPosBottomLeft.Size.Height);
            btnPosTopLeft.Location = new Point(0, 0);
            btnPosTopRight.Location = new Point(this.Size.Width - btnPosBottomLeft.Size.Width, 0);
        }

        private void radSmall_CheckedChanged(object sender, EventArgs e)
        {
            if (radSmall.Checked)
            {
                btnPosBottomLeft.Font = new Font("Webdings", 6);
                btnPosBottomRight.Font = new Font("Webdings", 6);
                btnPosTopLeft.Font = new Font("Webdings", 6);
                btnPosTopRight.Font = new Font("Webdings", 6);
                btnPosBottomLeft.Size = new Size(25, 25);
                btnPosBottomRight.Size = new Size(25, 25);
                btnPosTopLeft.Size = new Size(25, 25);
                btnPosTopRight.Size = new Size(25, 25);

                lblTime.Font = new Font("Ravie", 40);
                lblClose.Font = new Font("Cooper Black", 8);
                lblStopFadding.Font = new Font("Cooper Black", 8);

                radSmall.Location = new Point(12, 31);
                radBig.Location = new Point(453, 31);

                chkBounceBack.Font = new Font("Cooper Black", 8);
                chkBounceBack.Location = new Point(31, 90);

                chkAllowTransparency.Font = new Font("Cooper Black", 8);
                chkAllowTransparency.Location = new Point(340, 90);

                this.Size = new Size(482, 119);

                PositionCornerButton();
                PositionClose();
                PositionStopFadding();
                PlaceForm();
            }
        }

        private void radBig_CheckedChanged(object sender, EventArgs e)
        {
            btnPosBottomLeft.Font = new Font("Webdings", 10);
            btnPosBottomRight.Font = new Font("Webdings", 10);
            btnPosTopLeft.Font = new Font("Webdings", 10);
            btnPosTopRight.Font = new Font("Webdings", 10);
            btnPosBottomLeft.Size = new Size(35, 35);
            btnPosBottomRight.Size = new Size(35, 35);
            btnPosTopLeft.Size = new Size(35, 35);
            btnPosTopRight.Size = new Size(35, 35);

            lblTime.Font = new Font("Ravie", 80);
            lblClose.Font = new Font("Cooper Black", 12);
            lblStopFadding.Font = new Font("Cooper Black", 12);

            radSmall.Location = new Point(18, 41);
            radBig.Location = new Point(720, 41);

            chkBounceBack.Font = new Font("Cooper Black", 12);
            chkBounceBack.Location = new Point(41, 175);

            chkAllowTransparency.Font = new Font("Cooper Black", 12);
            chkAllowTransparency.Location = new Point(568, 175);

            this.Size = new Size(753, 210);

            PositionCornerButton();
            PositionClose();
            PositionStopFadding();
            PlaceForm();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.UseSmall = radSmall.Checked;
            Properties.Settings.Default.Save();
        }

        private void lblStopFadding_Click(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.KeepFading)
            {
                Properties.Settings.Default.KeepFading = false;
                Properties.Settings.Default.Save();
                lblStopFadding.Text = "[START FADDING]";
            }
            else
            {
                Properties.Settings.Default.KeepFading = true;
                Properties.Settings.Default.Save();
                lblStopFadding.Text = "[STOP FADDING]";
                f_FadeCompleted();
            }
            PositionStopFadding();
        }

        private void PositionStopFadding()
        {
            lblStopFadding.Location = new Point((this.Size.Width / 2) - (lblStopFadding.Size.Width / 2), 15);
        }

        private void lblStopFadding_MouseEnter(object sender, EventArgs e)
        {
            if (radSmall.Checked)
                lblStopFadding.Font = new Font("Cooper Black", 12);
            else
                lblStopFadding.Font = new Font("Cooper Black", 16);
            PositionStopFadding();
        }

        private void lblStopFadding_MouseLeave(object sender, EventArgs e)
        {
            if (radSmall.Checked)
                lblStopFadding.Font = new Font("Cooper Black", 8);
            else
                lblStopFadding.Font = new Font("Cooper Black", 12);
            PositionStopFadding();
        }

        private void chkBounceBack_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.BounceBack = chkBounceBack.Checked;
            Properties.Settings.Default.Save();
        }

        private void chkAllowTransparency_CheckedChanged(object sender, EventArgs e)
        {
            Properties.Settings.Default.AllowTransparency = chkAllowTransparency.Checked;
            Properties.Settings.Default.Save();
            if (chkAllowTransparency.Checked)
                this.TransparencyKey = Color.Black;
            else
                this.TransparencyKey = Color.Transparent;
        }
    }

    public enum Place
    {
        TopRight, TopLeft, BottomLeft, BottomRight
    }
}
